import {login, logout, getUserInfo} from '@/api/user'
import {setToken, getToken} from '@/libs/util'
import urlSource from "../../api/urlSource";
import axios from "axios/index";

export default {
  state: {
    userName: localStorage.getItem("setUserName"),
    userId: localStorage.getItem("setUserId"),
    avatorImgPath: '',
    token: getToken(),
    orgId: localStorage.getItem("setOrgId"),
    superFlag: localStorage.getItem("setSuperFlag"),
    logoUrl: localStorage.getItem("setLogoUrl"),
    logoTxt: localStorage.getItem("setLogoTxt"),
  },
  mutations: {
    setAvator(state, avatorPath) {
      state.avatorImgPath = avatorPath
    },
    setUserId(state, id) {
      state.userId = id
    },
    setUserName(state, name) {
      state.userName = name
    },
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setOrgId(state,id){
      state.orgId = id;
    },
    setSuperFlag(state,superFlag){
      state.superFlag = superFlag;
    },
    setLogoUrl(state,logoUrl){
      state.logoUrl = logoUrl;
    },
    setLogoTxt(state,logoTxt){
      state.logoTxt = logoTxt;
    },
  },
  actions: {
    // 登录
    handleLogin({commit}, {userName, password}) {
      return new Promise((resolve, reject) => {
        let url = urlSource.apiUrl + 'authorize/login';
        let data = {
          userName: userName,
          password: password
        }
        axios.post(url, data).then(res => {
          if (res.data.token != "OK") {
            reject(res.data)
          } else {
            // res.data.user.orgId = res.
            commit('setUserName', res.data.user.userName)
            commit('setUserId', res.data.user.id)
            commit('setOrgId',res.data.user.orgId);
            commit('setSuperFlag',res.data.user.superFlag);
            commit('setLogoUrl',res.data.logoUrl);
            commit('setLogoTxt',res.data.logoTxt);
            localStorage.setItem("setUserName", res.data.user.userName)
            localStorage.setItem("setUserId", res.data.user.id)
            localStorage.setItem("setOrgId", res.data.user.orgId)
            localStorage.setItem("setSuperFlag", res.data.user.superFlag)
            localStorage.setItem("setLogoUrl", res.data.logoUrl)
            localStorage.setItem("setLogoTxt", res.data.logoTxt)
            resolve(res.data)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut({state, commit}) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('setUserId', "")
          localStorage.setItem("setUserId", "")
          commit('setToken', '')
          localStorage.setItem("setToken", "")
          commit('setOrgId','')
          localStorage.setItem("setOrgId", "")
          commit('setSuperFlag','')
          localStorage.setItem("setSuperFlag", "")
          commit('setLogoUrl','');
          localStorage.setItem("setLogoUrl", "")
          commit('setLogoTxt','');
          localStorage.setItem("setLogoTxt", "");
          //清空之前选项卡
          localStorage.setItem("tagNaveList","");
          resolve()
        }).catch(err => {
          reject(err)
        })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        // commit('setToken', '')
        // commit('setAccess', [])
        // resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo({state, commit}) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(res => {
          const data = res.data
          commit('setAvator', data.avator)
          commit('setUserName', data.user_name)
          commit('setUserId', data.user_id)
          commit('setOrgId',data.orgId)
          commit('setSuperFlag',data.superFlag)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
